// 星门寻路

import axios from 'axios'
import { ICommandHandler } from '../types'

const findRoute: ICommandHandler = {
    commandName: '/route',
    async execute(sender) {
        if (!sender.command) return
        const [from, to] = sender.command.params
        if (!from || !to) {
            sender.reply(`请输入起点和终点，例如：/route shard3_E31S39 shard2_E19S31`)
            return
        }
        // 验证参数格式
        const regexp = /shard[0123]_[EW]\d+[SN]\d+/
        if (!regexp.test(from) || !regexp.test(to)) {
            sender.reply(`参数格式错误，正确格式如：/route shard3_E31S39 shard2_E19S31`)
            return
        }
        // 发送请求
        const instance = axios.create({
            timeout: 5000,
        })
        try {
            const response = await instance.get(
                `http://api-screeps.mofengfeng.com/api/portals/find_route?from=${from}&to=${to}`,
            )
            const { data } = response
            sender.reply(JSON.stringify(data))
        } catch (error) {
            sender.reply(`查询失败`)
        }
    },
}

export default findRoute
